package com.errands.cn.controller;

import com.errands.cn.entity.User;
import com.errands.cn.services.UserService;
import com.errands.cn.utils.Json;
import com.errands.cn.utils.MD5Util;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/get_user_page")
    @ResponseBody
    public PageInfo<User> getUserByPage(Integer pageNum,Integer pageSize){
        PageInfo<User> userPageInfo = userService.getUserByPage(pageNum,pageSize);
        return userPageInfo;
    }

    @PostMapping("/register")
    @ResponseBody
    public Json register(User user){
        Json json = new Json();
        if (user.getAccount() != null) {
            User user1 = userService.getUserByAccount(user.getAccount());
            if (user1 != null) {
                json.setMsg("该手机号已被注册");
                return json;
            }
        }
        user.setType(1);
        user.setPassword(MD5Util.md5(user.getPassword()));
        user.setHeadimg("default.jpg");
        if (userService.insertUser(user)){
            json.setSuccess(true);
            json.setMsg("注册成功,请登录！");
        }else {
            json.setMsg("注册失败！");
        }
        return json;
    }

    @PostMapping("/login")
    @ResponseBody
    public Json login(User user, HttpServletRequest request){
        Json json = new Json();
        if (user.getAccount() != null){
            String password = userService.getUserPassword(user.getAccount());
            if (password == null){
                json.setMsg("该用户未注册");
            }else {
                if (MD5Util.md5(user.getPassword()).equals(password)){
                    User user1 = userService.getUserByAccount(user.getAccount());
                    request.getSession().setAttribute("user",user1);
                    System.out.println(user1.getName()+"：用户登录");
                    json.setSuccess(true);
                    json.setObj("/index.html");
                }else {
                    json.setMsg("密码错误！");
                }
            }
        }
        return json;
    }

    @GetMapping("/loginout")
    @ResponseBody
    public boolean loginOut(HttpServletRequest request){
        try {
            request.getSession().removeAttribute("user");
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @GetMapping("/admin/delete_user")
    @ResponseBody
    public boolean deleteUser(@RequestParam("uid") Integer uid){
        try {
            userService.deleteUser(uid);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @PostMapping("/update_user")
    @ResponseBody
    public boolean updateUser(User user){
        try {
            System.out.println(user.getGender());
            userService.updateUser(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
